﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quotation
{
    /// <summary>
    /// 表示证券代码
    /// </summary>
    public struct SymbolCode
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="code">证券代码</param>
        /// <param name="exchId">市场ID</param>
        public SymbolCode(string code, char exchId)
        {
            this.code = code;
            this.exchId = exchId;
            this.longCode = exchId + code;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="codeWithExchIdPrefixed">包含市场ID的证券代码</param>
        public SymbolCode(string codeWithExchIdPrefixed)
        {
            if (string.IsNullOrEmpty(codeWithExchIdPrefixed)
                || codeWithExchIdPrefixed.Length < 1)
                throw new ArgumentException();
            this.exchId = codeWithExchIdPrefixed.First();
            this.code = codeWithExchIdPrefixed.Substring(1);
            this.longCode = codeWithExchIdPrefixed;
        }

        private char exchId;
        /// <summary>
        /// 市场ID
        /// </summary>
        public char ExchId
        {
            get { return this.exchId; }
            set
            {
                this.exchId = value;
                this.longCode = this.exchId + this.code;
            }
        }

        private string code;
        /// <summary>
        /// 证券代码
        /// </summary>
        public string Code
        {
            get { return this.code; }
            set
            {
                this.code = value;
                this.longCode = this.exchId + this.code;
            }
        }


        private string longCode;
        /// <summary>
        /// 包含市场ID的证券代码
        /// </summary>
        public string LongCode
        {
            get { return this.longCode; }
        }

        /// <summary>
        /// 重载运算符==
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool operator ==(SymbolCode a, SymbolCode b)
        {
            return a.LongCode == b.LongCode;
        }
        /// <summary>
        /// 重载运算符!=
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool operator !=(SymbolCode a, SymbolCode b)
        {

            return !(a == b);
        }
        /// <summary>
        /// 重写Equals方法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj is SymbolCode)
            {
                return this.LongCode == ((SymbolCode)obj).LongCode;
            }
            else
                return false;
        }
        /// <summary>
        /// 重写GetHashCode方法
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.LongCode.GetHashCode();
        }

        public override string ToString()
        {
            return this.LongCode;
        }
    }
}
